<?php

namespace App\Http\Controllers\Admin;

use App\Models\LevelTeacher;
use App\Models\Type;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;

/**
 * 用户分类管理
 * Class TypeController
 * @package App\Http\Controllers\Admin
 */
class TeacherLevelController extends BaseController
{
    /**
     * 分类列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {

        $list = LevelTeacher::paginate(config('site.other.paginate'));

        return backendView('level.index', compact('list'));
    }

    /**
     * 新增分类
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function add(Request $request)
    {
        if ($request->isMethod('POST')) {

            $this->validate($request, [
                'name' => 'required|max:255|unique:level_teachers,name',
                'level' => 'required|integer'
            ], [], [
                'name' => __("Name"),
                'level' => __("Level")
            ]);

            $data = $request->only(['name','level']);
            $data['tl_code'] = rand(100000,999999);
            if (LevelTeacher::create($data)) {
                return redirect()->route('admin.level.index')->with('success', __('Create success'));
            }

            return back()->with('error', __('Create failed'))->withInput();
        } else {
            return backendView('level.add');
        }
    }

    /**
     * 编辑分类
     * @param Request $request
     * @param null $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function edit(Request $request, $id = null)
    {
        /**
         * @var $level Level
         */
        $level = LevelTeacher::find($id);
        if (empty($level)) {
            return back()->with('error', __('The page you requested was not found'))->withInput();
        }

        if ($request->isMethod('POST')) {

            $this->validate($request, [
                'name' => ['required', 'max:255',Rule::unique('level_teachers')->ignore($level->id)],
                'level' => 'required|integer',
            ], [], [
                'name' => __("Name"),
                'level' => __("Level"),
            ]);

            $data = $request->only(['name','level']);

            if ($level->update($data)) {
                return redirect()->route('admin.level.index')->with('success', __('Edit success'));
            }

            return back()->with('error', __('Edit failed'))->withInput();

        } else {
            return backendView('level.edit', compact('level'));
        }
    }

    /**
     * 查看分类
     * @param null $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function view($id = null)
    {
        /**
         * @var $level Level
         */
        $level = Type::find($id);
        if (empty($level)) {
            return back()->with('error', __('The page you requested was not found'));
        }

        return backendView('type.view', compact('level'));
    }

    /**
     * 删除分类
     * @param null $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function delete($id = null)
    {
        $level = LevelTeacher::find($id);
        if (empty($level)) {
            return back()->with('error', __('The page you requested was not found'));
        }

        try {
            $teachers = $level->teachers()->get();

            if (count($teachers) > 0 ) {
                return back()->with('error', '头衔下存在教师，无法删除');
            }

            if ($level->delete()) {
                return back()->with('success', __('Delete success'));
            }
        } catch (\Exception $e) {
            return back()->with('error', $e->getMessage());
        }

        return back()->with('error', __('Delete failed'));
    }
}